package com.aaa.controller.api;

import com.aaa.aop.annotation.Log;
import com.aaa.entity.Dept;
import com.aaa.entity.Role;
import com.aaa.entity.User;
import com.aaa.service.RoleService;
import com.aaa.vo.Result;
import com.aaa.vo.RoleVo;
import com.baomidou.mybatisplus.core.metadata.IPage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("system/api/role")
public class RoleControllerApi {
    @Autowired
    private RoleService roleService;
    @GetMapping("/list")
    public Result<List<Role>> list(){
        return roleService.getAll();
    }

    @GetMapping("getByUserId/{userId}")
    public Result<Map<String,Object>> getByUserId(@PathVariable Long userId){
        return roleService.findByUserId(userId);
    }

    @PostMapping("confirmRole")
    @Log(title = "确认分配角色",business_type = "系统业务")
    public Result<String> confirmRole(Long userId,Long[] roleIds){
        return  roleService.confirmRole(userId,roleIds);
    }

    @PostMapping("list/{currentPage}/{pageSize}")
    public Result<IPage<Role>> list(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody RoleVo roleVo
    ){
        return roleService.getByPage(currentPage,pageSize,roleVo);
    }

    @DeleteMapping("delById/{id}")
    public Result delById(@PathVariable Long id){
        if (roleService.delById(id)){
            return new Result(200,"修改成功",true);
        }else {
            return  new Result(500,"修改失败",false);
        }
    }
    //添加删除
    @PostMapping("saveAndUpdate")
    public Result saveAndUpdate(Role role){
        if (roleService.saveAndUpdate(role)){
            return  new Result(200,"操作成功",true);
        }else {
            return new Result(500,"操作失败",false);
        }
    }

    //批量删除
    @PostMapping("delListById")
    public Result delListByid(@RequestBody List<Role> roleList){
        roleList.forEach((Role role)->{
            roleService.removeById(role.getRoleId());
        });
        return new Result(200,"批量删除成功",true);
    }
}
